<?php
#by reemo 2012
class ShowArsenalPage extends AbstractPage
{
	public static $requireModule = 0;

	function __construct() 
	{
		parent::__construct();
	}
	
	public function sendpr()
	{
		global $CONF, $pricelist, $resource, $reslist, $USER;
		
		
		$shipID			= HTTP::_GP('ship', 0);
		$greid			= HTTP::_GP('greid','');
		$tm				= HTTP::_GP('tm', 0);
		$am				= HTTP::_GP('am', 0);
		$Elements		= $reslist['upgrade_fleet'];
		
		
		if(!empty($shipID) && !empty($greid) && in_array($shipID, $Elements))
		{			
			$USER_arsenal	= $GLOBALS['DATABASE']->uniquequery("SELECT ".$greid."_".$shipID." FROM ".UP." WHERE id_owner = '".$USER['id']."';");
			
			if($USER_arsenal[$greid.'_'.$shipID] == 0)
			{
				$this->redirectTo('game.php?page=arsenal');
				return;
			}
			$tm = min(($shipID-200) * 10000 * $USER_arsenal[$greid.'_'.$shipID],$tm);
			$am = min(($shipID-200) * 1000 * $USER_arsenal[$greid.'_'.$shipID],$am);
			$GLOBALS['DATABASE']->query("UPDATE ".UP." SET ".$greid."_".$shipID." = 0 WHERE id_owner = ".$USER['id'].";");
			
			$SQLQuery = "INSERT INTO ".MUP." SET 
						 		`id_owner` = '".$USER['id']."', 
								`ship` = '".$shipID."', 
								`etap` = '".$USER_arsenal[$greid.'_'.$shipID]."', 
								`cena_tm` = '".$tm."', 
								`cena_am` = '".$am."', 
								`greid` = '".$greid."';";
			$GLOBALS['DATABASE']->query($SQLQuery);						
		
			$this->printMessage('Успешьно выставленно на прадажу', true, array("game.php?page=arsenal", 1));
		}
	}
	
	public function send()
	{
		global $CONF, $pricelist, $resource, $reslist, $USER;
		
		
		$shipID			= HTTP::_GP('ship', 0);
		$greid			= HTTP::_GP('greid','');
		$Elements		= $reslist['upgrade_fleet'];
		
		
		if(!empty($shipID) && !empty($greid) && in_array($shipID, $Elements))
		{			
			$USER_arsenal	= $GLOBALS['DATABASE']->uniquequery("SELECT ".$greid."_".$shipID." FROM ".UP." WHERE id_owner = '".$USER['id']."';");
			
			$GLOBALS['DATABASE']->query("UPDATE ".UP." SET ".$greid."_".$shipID." = 0 WHERE id_owner = ".$USER['id'].";");			
			$GLOBALS['DATABASE']->query("UPDATE ".UPA." SET ".$greid."_".$shipID." = ".$greid."_".$shipID." + ".$USER_arsenal[$greid.'_'.$shipID]." WHERE id_owner = ".$USER['id'].";");
			$this->printMessage('Успешьно активировано', true, array("game.php?page=arsenal", 1));
		}
	}
	
	function show()
	{
		global $CONF, $pricelist, $resource, $reslist, $USER, $CombatCaps;
		
		if( ($GLOBALS['DATABASE']->countquery("SELECT * FROM ".UP." WHERE id_owner = '".$USER['id']."';")) == 0)
		{
			$SQL =  "INSERT INTO ".UP." SET 
					id_owner	= ".$USER['id'].";
					INSERT INTO ".UPA." SET 
					id_owner	= ".$USER['id'].";";
			$GLOBALS['DATABASE']->multi_query($SQL);
		}
		
		$Elements		= $reslist['upgrade_fleet'];
		$arsenal = array();
		
		$USER_arsenal	= $GLOBALS['DATABASE']->uniquequery("SELECT * FROM ".UP." WHERE id_owner = '".$USER['id']."';");
		$USER_arsenal_a	= $GLOBALS['DATABASE']->uniquequery("SELECT * FROM ".UPA." WHERE id_owner = '".$USER['id']."';");
		
		foreach($Elements as $shipID)
		{
			$arsenal[$shipID]	= array(
				'a_level'			=> $USER_arsenal['a_'.$shipID],
				's_level'			=> $USER_arsenal['s_'.$shipID],
				'd_level'			=> $USER_arsenal['d_'.$shipID],
				'a_level_a'			=> $USER_arsenal_a['a_'.$shipID],
				's_level_a'			=> $USER_arsenal_a['s_'.$shipID],
				'd_level_a'			=> $USER_arsenal_a['d_'.$shipID],
				'a_bonus'			=> round($CombatCaps[$shipID]['attack']*($USER_arsenal_a['a_'.$shipID]/100)),
				's_bonus'			=> round($CombatCaps[$shipID]['shield']*($USER_arsenal_a['s_'.$shipID]/100)),
				'd_bonus'			=> round((($pricelist[$shipID]['cost'][901] + $pricelist[$shipID]['cost'][902])/10)*($USER_arsenal_a['d_'.$shipID]/100)),
			);
		}

		$this->tplObj->assign_vars(array(
			'arsenal'	=> $arsenal,
		));
		
		$this->display('page.arsenal.tpl');
	}
}
?>